#!/bin/bash

echo "=== 完整集群启动和应用程序部署 ==="
echo ""

# 1. 启动Hadoop集群
echo "1. 启动Hadoop集群..."
echo "启动HDFS..."
start-dfs.sh
sleep 5

echo "启动YARN..."
start-yarn.sh
sleep 5

echo "检查Hadoop状态..."
jps | grep -E "(NameNode|DataNode|ResourceManager|NodeManager)"
echo ""

# 2. 启动ZooKeeper（如果还没启动）
echo "2. 检查ZooKeeper状态..."
if ! jps | grep -q QuorumPeerMain; then
    echo "启动ZooKeeper..."
    zkServer.sh start
    sleep 3
fi
jps | grep QuorumPeerMain
echo ""

# 3. 启动HBase集群
echo "3. 启动HBase集群..."
echo "启动HBase..."
start-hbase.sh
sleep 10

echo "检查HBase状态..."
jps | grep -E "(HMaster|HRegionServer)"
echo ""

# 4. 验证HBase状态
echo "4. 验证HBase状态..."
hbase shell <<< "status"
echo ""

# 5. 构建应用程序
echo "5. 构建车辆召回数据分析应用..."
if command -v mvn &> /dev/null; then
    mvn clean package -DskipTests
    if [ $? -ne 0 ]; then
        echo "编译失败！"
        exit 1
    fi
else
    echo "Maven未安装，使用现有JAR文件..."
fi

echo "检查JAR文件..."
ls -la target/*.jar
echo ""

# 6. 启动应用程序
echo "6. 启动车辆召回数据分析应用..."
echo "应用将在以下地址运行:"
echo "  - 完整版界面: http://$(hostname -I | awk '{print $1}'):8080"
echo "  - 简化版界面: http://$(hostname -I | awk '{print $1}'):8080/index2.html"
echo ""

# 运行应用
java -jar target/hadoop-1.0-SNAPSHOT.jar

echo ""
echo "应用已退出" 